package com.jbpm.websercvice.action;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.emms.common.bean.PageBean;
import com.emms.common.constant.CommonEnumType;
import com.emms.common.constant.CommonEnumType.DisabledAndEnable;
import com.emms.common.exception.FrameworkException;
import com.emms.common.util.BaseUtil;
import com.emms.controller.baseinfo.vo.CodeInfoBean;
import com.jbpm.action.BaseAction;
import com.jbpm.extjsvo.JSONTreeNode;
import com.jbpm.util.EntityUtils;
import com.jbpm.util.JsonBuilder;
import com.jbpm.websercvice.manager.VCurrencyService;
import com.jbpm.websercvice.vo.ProjectInfoWfBean;
import com.jbpm.websercvice.vo.SectionWfBean;
import com.jbpm.websercvice.vo.SysRoleBean;
import com.jbpm.websercvice.vo.SysuserBean;
import com.jbpm.websercvice.vo.WFSystemBean;
import com.jbpm.websercvice.vo.WfModuleBean;
import com.jbpm.wf.model.WFModule;
import com.jbpm.wf.model.WFSystem;

@Component("websercviceAction")
@Scope("prototype")
public class WebsercviceAction extends BaseAction {
	
	private static final long serialVersionUID = 5512103430712332308L;
	
	//角色bean
	private SysRoleBean sysRoleBean;
	//用户bean
	private SysuserBean sysuserBean;
	
   //系统bean
   private WFSystemBean wFSystemBean;
   
   //项目bean
   private ProjectInfoWfBean projectInfoWfBean;
   
   //标段bean
   private SectionWfBean sectionWfBean;
   
   private PageBean pb;

    //业务模块bean
   private WfModuleBean wfModuleBean;
   
   //流程类型
   private String wfType;
   
	//Service
	@Resource(name = "vCurrencyServiceImpl")
	private VCurrencyService vCurrencyService;

	//父节点信息
	private String node; 
		
	/**
	 * 获取组织信息
	 * @throws FrameworkException 
	 */
	
	public void getLoadOrgTree() throws FrameworkException{
		if("666666".equals(node)){
			//首节点
			//组织类别
			CodeInfoBean cb=new CodeInfoBean();
			             cb.setType(CommonEnumType.CodeInfo.单位组织.getValue());
			             cb.setStatus(DisabledAndEnable.enable.getValue());
			//分页
			PageBean pb=new PageBean();
			pb.setLimit(-1);
			pb.setStart(-1);
			List<Object>  res=vCurrencyService.getLoadOrgTreeIndex(cb,pb);
			//返回页面信息
			List<JSONTreeNode> lists=new ArrayList<JSONTreeNode>();
			//循环父类
			for (Object ms:res) {
					CodeInfoBean m=(CodeInfoBean) ms;
					//节点
					JSONTreeNode node=new JSONTreeNode();
					//节点id
					node.setId(m.getId());
					//节点名称
					node.setText(m.getName());
					//编码
					if(!BaseUtil.isEmpty(m.getOrderNo())){
						node.setCode(m.getOrderNo().toString());
					}
					//提示信息
					node.setDescription(m.getRemark());      
					//是否展开
					node.setExpandable(true);
					//是否叶子
					node.setLeaf(false);
					//父节点
					node.setParent("666666");
					//获取子节点信息
					node=vCurrencyService.getLoadOrgBuildTreeModel(node,m.getId());
					lists.add(node);
			}
			//前台ExtJS 自动生成root [{},{}]
			toWrite(JsonBuilder.getInstance().buildObjListToJson(null, lists, false));
		}else{
			
		}
		
	}
	
	/**
	 * 所属业务模块数据获取
	 * 
	 * @param model
	 * @param wFModuleBean
	 * @param pb
	 * @return
	 * @throws FrameworkException
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void getModuleByParam() throws FrameworkException {
		Object model=new WFModule();
		if(EntityUtils.isNotNull(model)){
			this.whereSql=" and statuts='"+wfModuleBean.getStatuts()+"' ";
			this.orderSql=" order by orders asc ";
			whereSql+=orderSql;
			Integer count=pcManagerTemplate.getCount(model.getClass(), whereSql);
			List datas=pcManagerTemplate.findByPage(model.getClass(), whereSql, start, limit);
			strData=JsonBuilder.getInstance().buildObjListToJson((long)count,datas , true);
			toWrite(strData);
		}
		
		/*PageBean pbs = vCurrencyService.getModuleByParam(wfModuleBean, getPb());
		String strData = JsonBuilder.getInstance().buildObjListToJson(
				(long) pbs.getTotalProperty(), pbs.getRoot(), true);
		super.toWrite(strData);*/
	}
	
	/**
	 * 所属标段数据获取
	 * 
	 * @param model
	 * @param projectInfoWfBean
	 * @param pb
	 * @return
	 * @throws FrameworkException
	 */
	public void getSectionByParam() throws FrameworkException {
		PageBean pbs = vCurrencyService.getSectionByParam(sectionWfBean, getPb());
		String strData = JsonBuilder.getInstance().buildObjListToJson(
				(long) pbs.getTotalProperty(), pbs.getRoot(), true);
		super.toWrite(strData);
	}
	
	/**
	 * 所属项目数据获取
	 * 
	 * @param model
	 * @param projectInfoWfBean
	 * @param pb
	 * @return
	 * @throws FrameworkException
	 */
	public void getProjectByParam() throws FrameworkException {
		PageBean pbs = vCurrencyService.getProjectByParam(projectInfoWfBean, getPb());
		String strData = JsonBuilder.getInstance().buildObjListToJson(
				(long) pbs.getTotalProperty(), pbs.getRoot(), true);
		super.toWrite(strData);
	}
	
	/**
	 * 所属系统数据获取
	 * 
	 * @param model
	 * @param wFSystemBean
	 * @param pb
	 * @return
	 * @throws FrameworkException
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void getSystemByParam() throws FrameworkException {
		Object model=new WFSystem();
		if(EntityUtils.isNotNull(model)){
			if(BaseUtil.isEmpty(wFSystemBean)){
				wFSystemBean=new WFSystemBean();
				wFSystemBean.setStatuts("1");
			}
			this.whereSql=" and statuts='"+wFSystemBean.getStatuts()+"' ";
			this.orderSql=" order by orders asc ";
			whereSql+=orderSql;
			Integer count=pcManagerTemplate.getCount(model.getClass(), whereSql);
			List datas=pcManagerTemplate.findByPage(model.getClass(), whereSql, start, limit);
			strData=JsonBuilder.getInstance().buildObjListToJson((long)count,datas , true);
			toWrite(strData);
		}
		
		/*PageBean pbs = vCurrencyService.getSystemByParam(wFSystemBean, getPb());
		String strData = JsonBuilder.getInstance().buildObjListToJson(
				(long) pbs.getTotalProperty(), pbs.getRoot(), true);
		super.toWrite(strData);*/
	}
	
	/**
	 * 委托用户选择数据获取
	 * 
	 * @param model
	 * @param sysRoleBean
	 * @param pb
	 * @return
	 * @throws FrameworkException
	 */
	public void getUserByParam() throws FrameworkException {
		PageBean pbs = vCurrencyService.getUserByParam(sysuserBean, getPb());
		String strData = JsonBuilder.getInstance().buildObjListToJson(
				(long) pbs.getTotalProperty(), pbs.getRoot(), true);
		super.toWrite(strData);
	}
	
	/**
	 * 委托角色选择数据获取
	 * 
	 * @param model
	 * @param sysRoleBean
	 * @param pb
	 * @return
	 * @throws FrameworkException
	 */
	public void getRoleByParam() throws FrameworkException {
		PageBean pbs = vCurrencyService.getRoleByParam(sysRoleBean, getPb());
		String strData = JsonBuilder.getInstance().buildObjListToJson(
				(long) pbs.getTotalProperty(), pbs.getRoot(), true);
		super.toWrite(strData);
	}

	public SysRoleBean getSysRoleBean() {
		return sysRoleBean;
	}

	public void setSysRoleBean(SysRoleBean sysRoleBean) {
		this.sysRoleBean = sysRoleBean;
	}

	public PageBean getPb() {
		if(BaseUtil.isEmpty(pb)){
			this.pb=new PageBean();
		}
		pb.setStart(super.start);
		pb.setLimit(super.limit);
		return pb;
	}

	public void setPb(PageBean pb) {
		this.pb = pb;
	}

	@Override
	public Object getModel() {
		return null;
	}

	public SysuserBean getSysuserBean() {
		return sysuserBean;
	}

	public void setSysuserBean(SysuserBean sysuserBean) {
		this.sysuserBean = sysuserBean;
	}

	public WFSystemBean getwFSystemBean() {
		return wFSystemBean;
	}

	public void setwFSystemBean(WFSystemBean wFSystemBean) {
		this.wFSystemBean = wFSystemBean;
	}

	public ProjectInfoWfBean getProjectInfoWfBean() {
		return projectInfoWfBean;
	}

	public void setProjectInfoWfBean(ProjectInfoWfBean projectInfoWfBean) {
		this.projectInfoWfBean = projectInfoWfBean;
	}

	public SectionWfBean getSectionWfBean() {
		return sectionWfBean;
	}

	public void setSectionWfBean(SectionWfBean sectionWfBean) {
		this.sectionWfBean = sectionWfBean;
	}

	public WfModuleBean getWfModuleBean() {
		return wfModuleBean;
	}

	public void setWfModuleBean(WfModuleBean wfModuleBean) {
		this.wfModuleBean = wfModuleBean;
	}

	public String getWfType() {
		return wfType;
	}

	public void setWfType(String wfType) {
		this.wfType = wfType;
	}

	public String getNode() {
		return node;
	}

	public void setNode(String node) {
		this.node = node;
	}

	
}
